/*
* @Author: zhangfengjie
* @Date:   2016-04-01 16:57:34
* @Last Modified by:   SongGuangHui
* @Last Modified time: 2017-03-04 14:35:29
*/
/*
 *myScroll 存储实例化的iScroll对象
 * */
var myScroll,pullDownEl, pullDownOffset,pullUpEl, pullUpOffset;
/*---当返回的后台数据全部加载，调用该方法---*/
$(function()
{   console.log(total+'----'+firstRow)
    if(total <= firstRow)
    {
        console.log(1111);
        $(".pullUpIcon").hide();
        $(".pullUpLabel").html("没有更多了");
    }
});
var num = 1; //默认页面显示4条数据              num可变
/*-----插入新的内容------*/
/*function appendCont(n,obj,comment_list){
 var txt="";
 var m = n-num;
 for(var i=0;i<n;i++){
 txt += '<dl class="clearfix">\
 <dt class="fl">\
 <img src="'+comment_list[i].headimgurl+'"/>\
 </dt>\
 <dd class="fl">s\
 <div class="clearfix">\
 <h3 class="fl">'+comment_list[i].nickname+'</h3>\
 <span class="fr">'+comment_list[i].addtime+'</span>\
 </div>\
 <p>'+comment_list[i].contents+'</p>\
 </dd>\
 </dl>';
 num++;
 }
 obj.append(txt);
 myScroll.refresh();
 setTimeout(function(){
 myScroll.refresh();
 allData();
 },500);
 }*/
function pullUpAction (){
    var sourceNode = $(".circle_main").eq(0); // 获得被克隆的节点对象
    /* console.log(total+'----'+num*firstRow);*/
    console.log(total+'----'+num*firstRow)
    if(total > num*firstRow){
        $.post('/FrontEnjoy/topic_details', {"firstRow":num*firstRow,"topic_id":topic_id}, function(data, textStatus)
        {
            console.log(data);
            if (data != '' || data != null)
            {
                var len = data.length;
                var html = '';
                for (i = 0; i < len; i++)
                {console.log(data)
                    html += '<div class="circle_state">\
			        <div class="circle_state_user">\
			            <img src="'+data[i].headimgurl+'" alt="" />\
			            <h6>'+data[i].nickname+'</h6>\
			            <span>'+data[i].position+'</span>\
			        </div>\
			        <div class="circle_state_cont">\
			            <p>'+data[i].contents+'</p>\
			        </div>\
			        <div class="circle_state_img clearfix">';

                    if (data[i].imgs) {
                        var img_len = data[i].imgs.length;
                        if (img_len >= 3) {
                            for (var j = 0; j < img_len; j++) {
                                var class_name = '';
                                if (j == 0) {
                                    class_name = 'fl dl_img'
                                } else if (j == 1) {
                                    class_name = 'fr dr_img dr_one'
                                } else if (j == 2) {
                                    class_name = 'fr dr_img'
                                }
                                if (j <= 2) {
                                    html += '<img src="' + data[i].imgs[j] + '" class="dynamic_img ' + class_name + '" data-row="' + j + '" data-img="' + data[i].imgs[j] + '" />';
                                } else {
                                    html += '<div class="dynamic_img hide" data-row="' + j + '" data-img="' + data[i].imgs[j] + '"></div>';
                                }
                            }
                        } else if (img_len == 2) {
                            for(var j =0;j < img_len; j++){
                                html+='<img src="' + data[i].imgs[j] + '" class="dynamic_img fl two" data-row="'+ j +'"  data-img="' + data[i].imgs[j] + '" />';
                            }
                        }else if (img_len == 1){
                            for(var j =0;j < img_len; j++){
                                html+='<img src="' + data[i].imgs[j] + '" class="dynamic_img fl one" data-row="'+ j +'"  data-img="' + data[i].imgs[j] + '" />';
                            }
                        }
                    }
                    var like_class = '';
					var zanNum = Number(data[i].praise_num);
					if(data[i].is_praise == 1){
						like_class = 'like-select';
					}else{
						like_class = 'like-default';
					}
					if(zanNum>999){
						zanNum = "(999+)";
					}else{
						zanNum = "("+zanNum+")";
					}

                    var comment = '';
			if(data[i].comment_list != null){
				var comment_len = data[i].comment_list.length;
				var comments = data[i].comment_list;
				
				for(var z = 0; z < comment_len; z++){
					comment += '<li class="com_list clearfix">\
								<div class="list_title fl">\
									<span class="first_word name" >'+comments[z].from_nickname+'</span>';
									if(comments[z].reply_comment_id != 0){
										comment += '<span class="hf_word">回复</span><span class="last_word name">'+comments[z].to_nickname+'</span>'
									}
					comment += '：<span class="info_word">'+comments[z].contents+'</span>\
								</div>\
								<input type="text" class="show_keybord" data-user_id="{$comment.user_id}" data-comment_id="{$comment.comment_id}" />\
							</li>'
				}
			}
			
			html += '</div>\
			        <div class="circle_state_zan clearfix">\
			            <p class="">'+data[i].addtime+'</p>\
			            <div class="zan_icon" onclick="dianZan($(this))">\
			            	<i class="like '+like_class+'" data-id="'+data[i].view_id+'" data-praise_type="view"></i>\
			            	<span class="zan_num" data-zan="'+data[i].praise_num+'">'+zanNum+'</span>\
			            </div>\
			            <div class="comment_icon" data-id="'+data[i].view_id+'">\
							<span class="icon icon_pinglun3x"></span>\
							<input type="text" class="input_icon" data-id="'+data[i].view_id+'"/>\
						</div>\
			        </div>\
			        <ul class="comment_cont" data-id="'+data[i].view_id+'">'+
							comment
						+'</ul>';
              
             html += ' </div>';
          }
                sourceNode.append(html);
                setTimeout(function(){
                    myScroll.refresh();
                },500);
                num ++;
            }
        }, 'json');
    }
    else
    {
        $('.pullUpIcon').hide();
        $('.pullUpLabel').html('没有更多了');
    }
}
function loaded() {
  pullUpEl = document.getElementById('pullUp');
  pullUpOffset = pullUpEl.offsetHeight;
  myScroll = new iScroll('load_wrapper', {
       useTransition: true,
    topOffset: pullDownOffset,
    onRefresh: function () {
      if (pullUpEl.className.match('loading')) {
        pullUpEl.className = '';
        pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
      }
    },
    onScrollMove: function () {
    	$(".comment_input").blur();
    	$(".comment_input_wrap").addClass("comment_hide");
    	setTimeout(function(){
    		$(".submit_btn").show();
    	},100);
       if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
        pullUpEl.className = 'flip';
        pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始刷新...';
        this.maxScrollY = this.maxScrollY;
      } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
        pullUpEl.className = '';
        pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载...';
        this.maxScrollY = pullUpOffset;
      }
    },
    onScrollEnd: function () {
      if (pullUpEl.className.match('flip')) {
        pullUpEl.className = 'loading';
        pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';
        pullUpAction(); // ajax call?
      }
    }
  });
  setTimeout(function () { document.getElementById('load_wrapper').style.left = '0'; }, 300);
}
//初始化控件
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);
